﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
     /// <summary>
    /// 人员去向统计
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class LeaveStatisticsController: AdminBaseController
    {

        /// <summary>
        /// 人员去向统计
        /// </summary>
        /// <returns>视图</returns>
        [AuthorityActionFilter("m_LeaveStatistics", Order = 1)]
        [LogPageActionFilter("人员去向统计", Order = 2)]
        public ActionResult Index(string year, string month, string deptno)
        {
            if (year == null || year == "")
            {
                year = DateTime.Now.Year.ToString();
            }
            if (month == null || month == "")
            {
                month = DateTime.Now.Month.ToString();
            }
            ViewData["year"] = year;
            ViewData["month"] = month;
            if (deptno == null || deptno=="")
            {
                deptno = "";
            }
            ViewData["htmlstr"] = GetData(year, month, deptno);
            ViewData["mm"] = GetMonth();
            ViewData["yyyy"] = GetYYYY();
            ViewData["dw"] = GetDW();
            return View();
        }

        public ActionResult Save(string dw, string yyyy, string mm)
        {
            dw = Fetch.Post("dw_VI");
            yyyy = Fetch.Post("yyyy_VI");
            mm = Fetch.Post("mm_VI");
            if (yyyy == null || yyyy == "")
            {
                yyyy = DateTime.Now.Year.ToString();
            }
            if (mm == null || mm == "")
            {
                mm = DateTime.Now.Month.ToString();
            }
            ViewData["year"] = yyyy;
            ViewData["month"] = mm;
            if (dw == null || dw == "")
            {
                dw = "";
            }
            ViewData["htmlstr"] = GetData(yyyy, mm, dw);
            ViewData["mm"] = GetMonth();
            ViewData["yyyy"] = GetYYYY();
            ViewData["dw"] = GetDW();
            return View("Index");
        }

        /// <summary>
        /// 获取所有单位
        /// </summary>
        /// <returns>下拉框List</returns>
        public List<SelectListItem> GetDW()
        {
            BLL.HRDept HRDeptBLL = new BLL.HRDept();
            List<Model.HRDept> HRDeptList = HRDeptBLL.GetListBYFIsAgency(Common.GlobalConstant.ENTITYNO, "");
            List<SelectListItem> DWList = new List<SelectListItem>();
           foreach (Model.HRDept model in HRDeptList)
           {
                SelectListItem item = new SelectListItem();
                item.Value = model.FDeptNo;
                item.Text = model.FDeptName;
                DWList.Add(item);
            }
            return DWList;
        }

        /// <summary>
        /// 获取所有月份数据
        /// </summary>
        /// <returns>下拉框List</returns>
        public List<SelectListItem> GetMonth()
        {
            List<SelectListItem> MonthList = new List<SelectListItem>();
            for (int i = 1; i < 13; i++)
            {
                SelectListItem item = new SelectListItem();
                item.Value = i.ToString();
                item.Text = i + "月";
                MonthList.Add(item);
            }
            return MonthList;
        }

        /// <summary>
        /// 获取2007年到现在所有年份数据
        /// </summary>
        /// <returns>下拉框List</returns>
        public List<SelectListItem> GetYYYY()
        {
            List<SelectListItem> YYYYList = new List<SelectListItem>();
            for (int i = 2007; i < DateTime.Now.Year+1; i++)
            {
                SelectListItem item = new SelectListItem();
                item.Value = i.ToString();
                item.Text = i + "年";
                YYYYList.Add(item);
            }
            return YYYYList;
        }

        /// <summary>
        /// 获取前台显示Table
        /// </summary>
        /// <param name="year">年份</param>
        /// <param name="month">月份</param>
        /// <param name="deptno">部门编号（空为全部部门）</param>
        /// <returns>STR</returns>
        public string GetData(string year, string month,string deptno)
        {
            int count = 0;
            if (int.Parse(month) < DateTime.Now.Month)
            {
                count = DateTime.DaysInMonth(int.Parse(year), int.Parse(month));
            }
            else
            {
                count = DateTime.Now.Day;
            }

            DateTime beginData = DateTime.Parse(year + "-" + month + "-01");
            DateTime endData = DateTime.Parse(year + "-" + month + "-" + count);


            BLL.WorkCalendar WorkCalendarBLL = new BLL.WorkCalendar();
            List<Model.WorkCalendar> WorkCalendarList = WorkCalendarBLL.GetLeaveStatisticsList(beginData.ToString("yyyy-MM-dd"), endData.ToString("yyyy-MM-dd"));

            BLL.HREmplPosition HREmplPositionBLL = new BLL.HREmplPosition();

            DataTable empdt = HREmplPositionBLL.GetLeaveMainDeptEmplALL(Common.GlobalConstant.ENTITYNO);
            List<Model.HRDept> HRDeptList = new List<Model.HRDept>();
            BLL.HRDept HRDeptBLL = new BLL.HRDept();
            if (deptno == "")
            {
                HRDeptList = HRDeptBLL.GetListBYFIsAgencyProp(Common.GlobalConstant.ENTITYNO, "");
            }
            else
            {
                HRDeptList.Add(HRDeptBLL.GetModel(Common.GlobalConstant.ENTITYNO, deptno));
            }
            BLL.Leave LeaveBLL = new BLL.Leave();
            List<Model.Leave> LeaveList = LeaveBLL.GetLeaveStatisticsList(beginData.ToString("yyyy-MM-dd"), endData.ToString("yyyy-MM-dd"));

            string htmlStr = "<table width='95%' border='1' align='center' cellspacing='0' bordercolordark='#ffffff' bordercolorlight='#000000' cellpadding='4'>";
            htmlStr += @"<tr align='center'>
                        <td width='80'>部门</td> 
                        <td width='80'>姓名</td>";
            for (int i = 1; i <= WorkCalendarList.Count; i++)
            {
                htmlStr += " <td>" + i + "</td>";
            }
            if (WorkCalendarList.Count < DateTime.DaysInMonth(int.Parse(year), int.Parse(month)))
            {
                for (int i = WorkCalendarList.Count + 1; i <= DateTime.DaysInMonth(int.Parse(year), int.Parse(month)); i++)
                {
                    htmlStr += " <td>" + i + "</td>";
                }
            }

            htmlStr += "</tr>";

            foreach (Model.HRDept model in HRDeptList)
            {
                var tmpEmp = (from c in empdt.AsEnumerable()
                              where c["FDeptNo"].ToString() == model.FDeptNo
                              select c);
                foreach (var q in tmpEmp)
                {
                    htmlStr += @"<tr align='center'>
                        <td width='80'>" + model.FDeptName + @"</td> 
                        <td width='80'>" + q["FEmpName"] + @"</td>";
                    for (int i = 0; i < WorkCalendarList.Count; i++)
                    {
                        if (WorkCalendarList[i].Fdeftype == "1")
                        {
                            List<Model.Leave> selectLeaveList = (
                                from nm in LeaveList
                                where
                                    (nm.FBeginDate.ToShortDateString() == WorkCalendarList[i].FWorkDate.ToShortDateString()
                                    || nm.FEndDate.ToShortDateString() == WorkCalendarList[i].FWorkDate.ToShortDateString()
                                    || (DateTime.Parse(nm.FEndDate.ToShortDateString()) > DateTime.Parse(WorkCalendarList[i].FWorkDate.ToShortDateString()) && DateTime.Parse(nm.FBeginDate.ToShortDateString()) < DateTime.Parse(WorkCalendarList[i].FWorkDate.ToShortDateString())))
                                    && nm.FEmplNo == q["FEmplNo"].ToString()
                                select nm).ToList();
                            if (selectLeaveList.Count > 0)
                            {
                                htmlStr += @"<td align='center' style='cursor:hand'>";


                                bool istow = true; 
                                foreach (Model.Leave LeaveModel in selectLeaveList)
                                {
                                    if (LeaveModel.FClass == "2")
                                    {
                                        htmlStr += "<span title='" + LeaveModel.ShowDate + LeaveModel.FLeaveReasonName + "' style='font-size:18px' ><b>*</b></span>";

                                    }
                                    else
                                    {
                                        if (LeaveModel.FBeginDate.ToShortDateString() == WorkCalendarList[i].FWorkDate.ToShortDateString())
                                        {
                                            if (LeaveModel.FBeginDate.ToShortDateString() == LeaveModel.FEndDate.ToShortDateString())
                                            {
                                                string ss = LeaveModel.FBeginDate.ToString("HH:mm");
                                                string dd = LeaveModel.FBeginDate.ToString("yyyy-MM-dd");
                                                string ss2 = LeaveModel.FEndDate.ToString("HH:mm");
                                                string dd2 = LeaveModel.FEndDate.ToString("yyyy-MM-dd");
                                                BLL.WorkTime WorkTimeBLL = new BLL.WorkTime();
                                                Model.WorkTime WorkTimeModel = WorkTimeBLL.GetModelByDate(dd);
                                                if (ss2 != WorkTimeModel.FAMEndTimeStr)
                                                {
                                                    if (ss != WorkTimeModel.FAMBeginTimeStr)
                                                    {
                                                        htmlStr += "<span title='" + LeaveModel.ShowDate + LeaveModel.FLeaveReasonName + "'><b>▲</b></span>";
                                                    }
                                                    else
                                                    {
                                                        htmlStr += "<span title='" + LeaveModel.ShowDate + LeaveModel.FLeaveReasonName + "'><b>◆</b></span>";
                                                    }
                                                }
                                                else
                                                {
                                                    htmlStr += "<span title='" + LeaveModel.ShowDate + LeaveModel.FLeaveReasonName + "'><b>▲</b></span>";
                                                }
                                            }
                                            else
                                            {
                                                string ss = LeaveModel.FBeginDate.ToString("HH:mm");
                                                string dd = LeaveModel.FBeginDate.ToString("yyyy-MM-dd");
                                                BLL.WorkTime WorkTimeBLL = new BLL.WorkTime();
                                                Model.WorkTime WorkTimeModel = WorkTimeBLL.GetModelByDate(dd);
                                                if (ss == WorkTimeModel.FAMBeginTimeStr)
                                                {
                                                    htmlStr += "<span title='" + LeaveModel.ShowDate + LeaveModel.FLeaveReasonName + "'><b>◆</b></span>";
                                                }
                                                else
                                                {
                                                    htmlStr += "<span title='" + LeaveModel.ShowDate + LeaveModel.FLeaveReasonName + "'><b>▲</b></span>";
                                                }
                                            }
                                        }
                                        else if (LeaveModel.FEndDate.ToShortDateString() == WorkCalendarList[i].FWorkDate.ToShortDateString())
                                        {
                                            string ss = LeaveModel.FEndDate.ToString("HH:mm");
                                            string dd = LeaveModel.FEndDate.ToString("yyyy-MM-dd");
                                            BLL.WorkTime WorkTimeBLL = new BLL.WorkTime();
                                            Model.WorkTime WorkTimeModel = WorkTimeBLL.GetModelByDate(dd);
                                            if (ss != WorkTimeModel.FAMEndTimeStr)
                                            {
                                                htmlStr += "<span title='" + LeaveModel.ShowDate + LeaveModel.FLeaveReasonName + "'><b>◆</b></span>";
                                            }
                                            else
                                            {
                                                htmlStr += "<span title='" + LeaveModel.ShowDate + LeaveModel.FLeaveReasonName + "'><b>▲</b></span>";
                                            }

                                        }
                                        else
                                        {
                                            htmlStr += "<span title='" + LeaveModel.ShowDate + LeaveModel.FLeaveReasonName + "'><b>◆</b></span>";
                                        }
                                        istow = false;
                                    }
                                }
                                if (istow)
                                {

                                    if (WorkCalendarList[i].FWorkDate >= DateTime.Now)
                                    {
                                        htmlStr += @"&nbsp;&nbsp;";
                                    }
                                    else
                                    {
                                        htmlStr += "<span title='无请假或外出'  style='font-size:22px'>○</span>";
                                    }
                                   
                                }
                                htmlStr += "</td>";
                            }
                            else
                            {
                                if (WorkCalendarList[i].FWorkDate >= DateTime.Now)
                                {
                                    htmlStr += @"<td align='center' style='cursor:hand'> 
                                             &nbsp;&nbsp;</td>";
                                }
                                else
                                {
                                    htmlStr += @"<td align='center' style='cursor:hand'> 
                                             <span title='无请假或外出' style='font-size:22px'>○</span></td>";
                                }
                                
                            }
                        }
                        else
                        {
                            htmlStr += @"<td align='center' style='cursor:hand'>◎</td>";
                        }
                    }

                    if (WorkCalendarList.Count <DateTime.DaysInMonth(int.Parse(year), int.Parse(month)))
                    {
                        DateTime begindatezy = endData.AddDays(1);
                        DateTime endDatazy = DateTime.Parse(year + "-" + month + "-" + DateTime.DaysInMonth(int.Parse(year), int.Parse(month)));

                        List<Model.WorkCalendar> WorkCalendarListzy = WorkCalendarBLL.GetLeaveStatisticsList(begindatezy.ToString("yyyy-MM-dd"), endDatazy.ToString("yyyy-MM-dd"));

                        for (int i = 0; i < WorkCalendarListzy.Count; i++)
                        {
                            if (WorkCalendarListzy[i].Fdeftype != "1")
                            {
                                htmlStr += @"<td align='center' style='cursor:hand'>◎</td>";
                            }
                            else
                            {
                                htmlStr += "<td>&nbsp;</td>";
                            }
                        }
                    }

                    htmlStr += "</tr>";
                }

            }
            htmlStr += "</table>";
            return htmlStr;
        }

    }
}
